<?php
// 启动会话
session_start();

// 从前端获取数据
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$roomType = $_POST['roomType'];
$checkInDate = $_POST['checkInDate'];
$checkOutDate = $_POST['checkOutDate'];
$message = $_POST['message'];
$submitTime = $_POST['submitTime'];
$account_phone = $_SESSION['phone'];
// 数据库连接配置
// 连接数据库
require_once 'databaseConnection.php';

try {
    // 创建数据库连接
    $conn = connectToDatabase();
    // 设置 PDO 错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 准备 SQL 语句，将数据插入数据库的 guestbook 表
    $sql = "INSERT INTO guestbook (name, phone, email, roomType, checkInDate, checkOutDate,account_phone, message, submitTime) 
            VALUES (:name, :phone, :email, :roomType, :checkInDate, :checkOutDate, :account_phone, :message, :submitTime)";
    $stmt = $conn->prepare($sql);

    // 绑定参数，并执行 SQL 语句
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':phone', $phone);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':roomType', $roomType);
    $stmt->bindParam(':checkInDate', $checkInDate);
    $stmt->bindParam(':checkOutDate', $checkOutDate);
    $stmt->bindParam(':account_phone', $account_phone);
    $stmt->bindParam(':message', $message);
    $stmt->bindParam(':submitTime', $submitTime);
    $stmt->execute();

    // 返回成功消息给前端
    echo "数据上传成功";
} catch(PDOException $e) {
    // 返回错误消息给前端
    echo "数据上传失败: " . $e->getMessage();
}

// 关闭数据库连接
$conn = null;
?>